/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exatrez.manager;

import com.google.inject.Inject;
import com.wideplay.warp.persist.Transactional;
import exatrez.dao.PriorityCodeDAO;
import exatrez.model.PriorityCode;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author wan.fairul
 */
public class PriorityManager {

    @Inject
    protected com.google.inject.Provider<Session> sessionProvider;
    @Inject
    private PriorityCodeDAO priorityCodeDAO;

    @Transactional
    public void saveOrUpdate(PriorityCode priorityCode) {
        priorityCodeDAO.saveOrUpdate(priorityCode);
    }

    @Transactional
    public void deletePriority(PriorityCode priorityCode) {
        priorityCodeDAO.delete(priorityCode);
    }

    public PriorityCode findPriorityCode(String priorityName) {
        String query = "SELECT a FROM PriorityCode a WHERE a.priorityName = :priorityName  ";
        Query q = sessionProvider.get().createQuery(query);
        q.setString("priorityName", priorityName);
        return (PriorityCode) q.uniqueResult();
    }
    
     public PriorityCode findNotPriorityName(String priorityCodeId, String priorityName) {
        String query = "SELECT a FROM PriorityCode a WHERE a.priorityCodeId != :priorityCodeId and a.priorityName = :priorityName  ";
        Query q = sessionProvider.get().createQuery(query);
        q.setString("priorityCodeId", priorityCodeId);
        q.setString("priorityName", priorityName);
        return (PriorityCode) q.uniqueResult();
    }
    
        public List<PriorityCode> findAll() {
        String query = "SELECT a FROM PriorityCode a WHERE a.priorityCodeId is not null  ";
        Query q = sessionProvider.get().createQuery(query);
        return q.list();
    }
        
        public String getPriorityDay(String priorityCodeId) {
         String result = "0";
         String query = "SELECT a.priorityDay FROM PriorityCode a WHERE a.priorityCodeId = :id";
         Query q = sessionProvider.get().createQuery(query).setString("id", priorityCodeId);
         Object obj = q.uniqueResult();
         if (obj!=null) {
             result = String.valueOf(obj);
         }
         return result;
     }

    public PriorityCodeDAO getPriorityCodeDAO() {
        return priorityCodeDAO;
    }

    public void setPriorityCodeDAO(PriorityCodeDAO priorityCodeDAO) {
        this.priorityCodeDAO = priorityCodeDAO;
    }    
}
